/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.inspur.edp.metadata.service.dev.webapi;

import com.inspur.edp.lcm.metadata.api.exception.ErrorCodes;
import com.inspur.edp.lcm.metadata.api.exception.LcmMetadataDevException;
import com.inspur.edp.lcm.metadata.devcommon.ManagerUtils;
import com.inspur.edp.metadata.service.dev.core.entity.validate.MetadataValidateResult;
import com.inspur.edp.metadata.service.dev.core.manager.MetadataValidateManager;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;
import org.springframework.util.StringUtils;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

/**
 * 元数据关系校验rest接口
 *
 * @author sunhongfei
 */

@Path("/")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class MetadataValidateWebApi {

    private MetadataValidateManager metadataValidateManager;

    private MetadataValidateManager getMetadataValidateManager() {
        if (metadataValidateManager == null) {
            metadataValidateManager = SpringBeanUtils.getBean(MetadataValidateManager.class);
        }
        return metadataValidateManager;
    }

    @POST
    @Path("/validatebo")
    public MetadataValidateResult metadataValidate(@QueryParam(value = "boPath") String boPath) {
        // 校验传参非空
        if (!StringUtils.hasText(boPath)) {
            throw new LcmMetadataDevException(ErrorCodes.ECP_METADATA_DEV_0005, "boPath");
        }
        // 获取应用路径的绝对路径
        String boAbsolutePath = ManagerUtils.getAbsolutePath(boPath);

        return getMetadataValidateManager().validateMetadatasInBo(boAbsolutePath);
    }

    @GET
    @Path("/generategraph")
    public String generateGraph(@QueryParam(value = "boPath") String boPath) {
        // 校验传参非空
        if (!StringUtils.hasText(boPath)) {
            throw new LcmMetadataDevException(ErrorCodes.ECP_METADATA_DEV_0005, "boPath");
        }
        // 获取应用路径的绝对路径
        String boAbsolutePath = ManagerUtils.getAbsolutePath(boPath);

        getMetadataValidateManager().generateMetadataGraphInBo(boAbsolutePath);
        return null;
    }
}
